package com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.back.bizactions;

import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.svc.document.storage.api.DocStorageService;
import com.inspur.edp.svc.document.storage.entity.GspDocMetadata;
import com.inspur.edp.svc.document.storage.entity.GspDocOperatingModes;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.rpc.client.RpcClassHolder;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.util.*;

public class sbejbyfkupLoadFjMgrAction extends AbstractManagerAction<String> {
    private String id;
    private ArrayList<Object> picList;

    public sbejbyfkupLoadFjMgrAction(IBEManagerContext managerContext, String id, ArrayList<Object> picList) {
        super(managerContext);
        this.id = id;
        this.picList = picList;
    }

    @Override
    public void execute() {
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        String configID = "4965c2fe-3cf3-4808-b468-4ed965091153";
        RpcClassHolder rpcClassHolder = SpringBeanUtils.getBean(RpcClassHolder.class);
        DocStorageService docStorage = rpcClassHolder.getRpcClass(DocStorageService.class);
        String rootId = "maintenance";
        try {
            //获取对应的保养明细
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp lcp = iLcpFactory.createLcpByBEId(configID);
            IEntityData currData = lcp.retrieve(id).getData();
            String bybw = currData.getValue("bybw").toString(); //保养部位
//            String byxm = currData.getValue("byxm").toString(); //保养项目

            int index = 0;
            for (Object pic : picList) {
                index++;
                List<Integer> picList = (List<Integer>) pic;

                // 获取前端传递的图片流 转成 byte[]
                Byte[] sbbhs = new Byte[picList.size()];
                for (int i = 0; i < picList.size(); i++) {
                    sbbhs[i] = (byte) (picList.get(i) & 0xFF);
                }

                GspDocMetadata metadata = new GspDocMetadata(rootId);
                String fileName = bybw + "_" + index;
                metadata.setFileName(fileName);
                metadata.setDocType("png");
                metadata.setDocSize(String.valueOf(sbbhs.length));
                byte[] bytes = toPrimitives(sbbhs);
                String attachmentId = docStorage.uploadDoc(fileName, metadata, bytes, GspDocOperatingModes.Formal);

                // 拼接参数，直接将图片关联到data上
                HashMap<String, Object> map = new HashMap<>();
                map.put("AttachmentId", attachmentId);
                map.put("FileName", metadata.getFileName() + ".png");
                map.put("FileSize", new BigDecimal(metadata.getDocSize()));
                map.put("FileCreateTime", new Date());

                ArrayList<String> nodeCodes = new ArrayList<>();
                nodeCodes.add("fjInfo");
                ArrayList<String> hierarchyIds = new ArrayList<>();
                hierarchyIds.add(id);
                IEntityData fjInfo = lcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

                ModifyChangeDetail dataChange = new ModifyChangeDetail(id);
                ModifyChangeDetail mxChange = new ModifyChangeDetail(fjInfo.getID());
                mxChange.putMultiUdtChange("fjInfo", map);

                dataChange.addChildChangeSet("fjInfo", mxChange);

                lcp.modify(dataChange);
            }

            lcp.save();
            setResult("上传成功");

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }

    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }

    public byte[] toPrimitives(Byte[] oBytes) {
        return Arrays.stream(oBytes).mapToInt(Byte::intValue) // 先转 int
                .collect(ByteArrayOutputStream::new, (baos, val) -> baos.write((byte) val), (baos1, baos2) -> {
                }).toByteArray();
    }
}